<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href>商品分类</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card v-loading="loading">
      <!-- 行     gutter是格式这里的间隔-->
      <el-row :gutter="20">
        <!-- 列 -->
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getCategoryList">
            <el-button slot="append" icon="el-icon-search" @click="getCategoryList"></el-button>
          </el-input>
        </el-col>
        <!-- 列 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="categoryList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="图片" width="100px">
          <template slot-scope="scope">
            <el-image :src="scope.row.url"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="分类名称" prop="name"></el-table-column>
        <el-table-column label="排列顺序" prop="index"></el-table-column>
        <el-table-column label="操作" width="130px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row._id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsById(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
       <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getGoodsCategoryList, delCategoryByid } from '../../api/category/list.js'
export default {
  data() {
    return {
      loading: false,
      queryInfo: {
        query: null,
        // 当前的页数
        pageNum: 1,
        // 当前每页显示多少条
        pageSize: 5
      },
      categoryList: [],
      total: 0
    }
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    getCategoryList() {
      this.loading = true
      getGoodsCategoryList(this.queryInfo).then(res => {
        console.log(res)
        if (res.data.success) {
          this.categoryList = res.data.data
          this.total = res.data.total
          console.log('total:' + this.total)
          this.loading = false
        }
        console.log(this.categoryList)
      })
    },
    goAddPage() {
      this.$router.push('/category/add')
    },
    showEditDialog(id) {
      console.log(id)
      this.$router.push('/category/update/?id=' + id)
    },
    removeGoodsById(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该商品分类, 是否继续?', '删除分类提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        console.log(res)
        if (res !== 'confirm') return
        this.loading = true
        delCategoryByid(id).then(res => {
          console.log(res.data)
          this.loading = false
          if (res.data.deleted === 0) return this.$message.error('删除分类失败')
          // 重新获取用户列表
          this.getCategoryList()
          this.$message.success('删除指定分类完成')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 监听 pageSize 改变事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.getCategoryList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryInfo.pageNum = newPage
      //   console.log(this.queryInfo.pageNum)
      this.getCategoryList()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
